<?php
/**
 * 执行数据库迁移：添加 unit_points 字段
 */

$host = '113.44.136.17';
$port = '3306';
$dbname = 'qiandao';
$username = 'qiandao';
$password = 'NGbks3HWx5PhSxDX';

try {
    $pdo = new PDO("mysql:host={$host};port={$port};dbname={$dbname};charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    echo "=== 数据库迁移：添加 unit_points 字段 ===\n\n";

    // 1. 检查字段是否已存在
    $stmt = $pdo->query("SHOW COLUMNS FROM `checkin_setting` LIKE 'unit_points'");
    $exists = $stmt->fetch();

    if ($exists) {
        echo "⚠️  字段 unit_points 已存在，跳过添加\n\n";
    } else {
        echo "📝 添加字段 unit_points...\n";
        $pdo->exec("
            ALTER TABLE `checkin_setting` 
            ADD COLUMN `unit_points` int(11) NOT NULL DEFAULT '1' 
            COMMENT '每个单位记多少分（通用积分）' 
            AFTER `unit_minutes`
        ");
        echo "✅ 字段 unit_points 添加成功\n\n";
    }

    // 2. 修改 unit_minutes 字段注释
    echo "📝 修改字段 unit_minutes 注释...\n";
    $pdo->exec("
        ALTER TABLE `checkin_setting` 
        MODIFY COLUMN `unit_minutes` int(11) NOT NULL DEFAULT '10' 
        COMMENT '每满N分钟为1个单位'
    ");
    echo "✅ 字段 unit_minutes 注释修改成功\n\n";

    // 3. 查看当前配置
    echo "📊 当前配置：\n";
    $stmt = $pdo->query("SELECT * FROM `checkin_setting` WHERE store_id = 0");
    $config = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($config) {
        echo "  unit_minutes: {$config['unit_minutes']} (每满{$config['unit_minutes']}分钟为1个单位)\n";
        echo "  unit_points: {$config['unit_points']} (每个单位记{$config['unit_points']}分)\n";
        echo "  rounding: {$config['rounding']}\n";
        echo "  timeout_minutes: {$config['timeout_minutes']}\n";
        echo "  time_slots: {$config['time_slots']}\n";
    } else {
        echo "  ⚠️  未找到配置记录\n";
    }

    echo "\n=== 迁移完成 ===\n";

} catch (Exception $e) {
    echo "❌ 错误: " . $e->getMessage() . "\n";
    exit(1);
}

